#include "stdafx.h"

#include "vector2.h"

Vector2::Vector2( float X, float Y )
{
    x = X;
    y = Y;
}
Vector2::~Vector2() {}

void Vector2::operator+= ( Vector2 &vect )
{
    x += vect.x; y += vect.y;
}
void Vector2::operator-= ( Vector2 &vect )
{
    x -= vect.x; y -= vect.y;
}
void Vector2::operator*= ( float scalar )
{
    x *= scalar; y *= scalar;
}
void Vector2::operator/= ( float scalar )
{
    x /= scalar; y /= scalar;
}
void Vector2::operator+= ( float scalar )
{
    x += scalar; y += scalar;
}
Vector2 Vector2::operator* ( const float scalar ) const
{
    return Vector2( x * scalar, y * scalar );
}
Vector2 Vector2::operator+ ( const Vector2 &vect ) const
{
    return Vector2( x + vect.x, y + vect.y );
}
Vector2 Vector2::operator- ( const Vector2 &vect ) const
{
    return Vector2( x - vect.x, y - vect.y );
}

void Vector2::AddX( float scalar )
{
    x += scalar;
}

void Vector2::AddY( float scalar )
{
    y += scalar;
}

void Vector2::Rotate( const float angle )
{
    float xt = (x * cosf( angle )) - (y * sinf( angle ));
    float yt = (y * cosf( angle )) + (x * sinf( angle ));

    x = xt;
    y = yt;
}

void Vector2::Normalize( void )
{
    float mag = sqrtf( x * x + y * y );

    x /= mag;
    y /= mag;
}

void Vector2::Flip( void )
{
    x *= -1.0f;
    y *= -1.0f;
}

float Vector2::Dot( const Vector2 &vect ) const
{
    return (x * vect.x) + (y * vect.y);
}

float Vector2::Dist( const Vector2 &vect ) const
{
    float dx = vect.x - x;
    float dy = vect.y - y;

    return sqrt( dx * dx + dy * dy );
}

float Vector2::Cross( const Vector2 &vect2 ) const
{
    return (x * vect2.y) - (y * vect2.x);
}

float Vector2::Magnitude( void )
{
    return sqrtf( x * x + y * y );
}

float Vector2::MagnitudeSquared( void )
{
    return x * x + y * y;
}

Vector2 Vector2::GetPerpendicular( void )
{
    return Vector2( -y, x );
}

Vector2 Vector2::GetConnection( Vector2 v )
{
    return Vector2( v.x - x, v.y - y);
}

Vector2 Vector2::GetComponentOnto( Vector2 &vect )
{
    float dot = Dot( vect ) / vect.Dot( vect );

    return vect * dot;
}